package cn.tedu.property_management_system.controller;


import cn.tedu.property_management_system.entity.Building;
import cn.tedu.property_management_system.entity.Meter;
import cn.tedu.property_management_system.entity.dto.MeterDTO;
import cn.tedu.property_management_system.entity.VO.MeterVO;
import cn.tedu.property_management_system.entity.dto.MeterSO;
import cn.tedu.property_management_system.service.IMeterService;
import cn.tedu.property_management_system.utils.JsonPage;
import cn.tedu.property_management_system.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/meter")
public class MeterController {
    @Autowired
    IMeterService service;

    @PostMapping("/select/house/type")
    public JsonResult<List<MeterSO>> selectMeter(@RequestBody MeterDTO meterDTO) {
        System.out.println(meterDTO);
        List<MeterSO> meter = service.selectMeter(meterDTO);
        System.out.println(meter);
        return JsonResult.OK(meter);
    }

    @PostMapping("/select/list/meter")
    public JsonResult<List<MeterSO>> selectListMeter() {


        List<MeterSO> meters = service.selectListMeter();
        System.out.println(meters);

        return JsonResult.OK(meters);
    }

    @PostMapping("/insert/meter")
    public JsonResult<Void> insertMeter(@RequestBody MeterVO meterVO) {
        service.addMeter(meterVO);
        return JsonResult.OK();
    }


    @GetMapping("/delete/meter/{id}")
    public JsonResult<Void> deleteMeter(@PathVariable("id") Long id) {
        System.out.println(id+"___________");
        int row = service.deleteMeter(id);
        System.out.println(row);
        return JsonResult.OK();
    }

    @PostMapping("/delete/meters")
    public JsonResult<Void> deleteMeters(@RequestBody List<Meter> meters) {
        List<Long> ids = new ArrayList<>();
        for (Meter meter : meters) {
            ids.add(meter.getId());
        }
        service.deleteMeters(ids);
        return JsonResult.OK();
    }


    @PostMapping("/select/houses")
    public JsonResult<String> selectHouses() {

        List<String> houses = service.selectHouse();


        return JsonResult.OK(houses);
    }


    @GetMapping("/get/page/{pageSize}/{pageIndex}")
    public JsonPage<MeterSO> getPage(@PathVariable("pageSize") Integer pageSize, @PathVariable("pageIndex") Integer pageIndex) {
        log.debug(pageSize+"-----"+pageIndex);
        JsonPage<MeterSO> buildingJsonPage = service.pageSelect(pageSize, pageIndex);
        return buildingJsonPage;
    }



}
